package com.sanwu.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanwu.dto.SysUserDto;
import com.sanwu.entity.SysUser;
import com.sanwu.mapper.SysUserMapper;
import com.sanwu.service.SysUserService;
import com.sanwu.service.UtilService;
import com.sanwu.vo.AjaxResult;
import com.sanwu.vo.CommonMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author hgh
 * @date 2025/3/11
 * @apiNote
 */

@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private UtilService utilService;

    /**
     * 登录
     *
     * @param sysUserDto
     * @return
     */
    @Override
    public AjaxResult passWordLogin(SysUserDto sysUserDto) {
        //查询数据库是否有用户名、密码相同的账号
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getIsDelete, 0);
        queryWrapper.eq(SysUser::getAccountNumber, sysUserDto.getAccountNumber());

        if (sysUserMapper.selectList(queryWrapper) == null || sysUserMapper.selectList(queryWrapper).size() <= 0) {
            return new AjaxResult(CommonMessage.unregistered, "当前用户未注册");
        }

        queryWrapper.eq(SysUser::getPassWord, sysUserDto.getPassWord());
        List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper);
        if (sysUsers == null || sysUsers.size() <= 0) {
            return new AjaxResult(CommonMessage.passwordError, "用户密码输入错误");
        }

        return new AjaxResult(CommonMessage.success, "登录成功", sysUsers.get(0));
    }

    /**
     * 短信验证码登录
     *
     * @param sysUserDto
     * @return
     */
    @Override
    public AjaxResult phoneMessageLogin(SysUserDto sysUserDto) {
        //查询数据库是否有用户名、密码相同的账号
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getIsDelete, 0);
        queryWrapper.eq(SysUser::getAccountNumber, sysUserDto.getAccountNumber());
        List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper);

        if (sysUsers == null || sysUsers.size() <= 0) {
            return new AjaxResult(CommonMessage.unregistered, "当前用户未注册");
        }

        //短信验证码校验
        if (utilService.checkPhoneMessage()) {
            return new AjaxResult(CommonMessage.phoneMessageError, "短信验证码错误");
        }

        return new AjaxResult(CommonMessage.success, "登录成功", sysUsers.get(0));
    }

    /**
     * 查看用户详情
     *
     * @param sysUserDto
     * @return
     */
    @Override
    public AjaxResult getDetail(SysUserDto sysUserDto) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getIsDelete, 0);
        queryWrapper.eq(SysUser::getId, sysUserDto.getId());
        List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper);

        if (sysUsers == null || sysUsers.size() <= 0) {
            return new AjaxResult(CommonMessage.error, "查询失败");
        }

        return new AjaxResult(CommonMessage.success, "查询成功", sysUsers.get(0));
    }

    /**
     * 修改用户信息
     *
     * @param sysUserDto
     * @return
     */
    @Override
    public AjaxResult updateUserInfo(SysUserDto sysUserDto) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getIsDelete, 0);
        queryWrapper.eq(SysUser::getAccountNumber, sysUserDto.getAccountNumber());

        if (sysUserMapper.selectList(queryWrapper) != null && sysUserMapper.selectList(queryWrapper).size() > 0) {
            return new AjaxResult(CommonMessage.accountNumberError, "当前用户账号已存在，请检查");
        }

        queryWrapper.eq(SysUser::getPhone, sysUserDto.getPhone());
        if (sysUserMapper.selectList(queryWrapper) != null && sysUserMapper.selectList(queryWrapper).size() > 0) {
            return new AjaxResult(CommonMessage.phoneError, "当前手机号已存在，请检查");
        }

        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUser::getIsDelete, 0);
        updateWrapper.eq(SysUser::getId, sysUserDto.getId());

        //基础信息赋值
        int update = sysUserMapper.update(this.dealBasicInfo(sysUserDto), updateWrapper);

        return update >= 1 ? new AjaxResult(CommonMessage.success, "修改成功")
                : new AjaxResult(CommonMessage.error, "修改失败");
    }

    /**
     * 注册用户
     *
     * @param sysUserDto
     * @return
     */
    @Override
    public AjaxResult insertUser(SysUserDto sysUserDto) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getIsDelete, 0);
        queryWrapper.eq(SysUser::getAccountNumber, sysUserDto.getAccountNumber());

        if (sysUserMapper.selectList(queryWrapper) != null && sysUserMapper.selectList(queryWrapper).size() > 0) {
            return new AjaxResult(CommonMessage.accountNumberError, "当前用户账号已存在，请检查");
        }

        queryWrapper.eq(SysUser::getPhone, sysUserDto.getPhone());
        if (sysUserMapper.selectList(queryWrapper) != null && sysUserMapper.selectList(queryWrapper).size() > 0) {
            return new AjaxResult(CommonMessage.phoneError, "当前手机号已存在，请检查");
        }

        //基础信息赋值
        SysUserDto entity = this.dealBasicInfo(sysUserDto);
        int update = sysUserMapper.insert(entity);

        return update >= 1 ? new AjaxResult(CommonMessage.success, "修改成功")
                : new AjaxResult(CommonMessage.error, "修改失败");
    }

    /**
     * 发送短信验证码
     * @return
     */
    @Override
    public AjaxResult sendCheckCode() {
        return new AjaxResult(200,"","123456");
    }

    /**
     * 基础信息赋值
     *
     * @param sysUserDto
     * @return
     */
    private SysUserDto dealBasicInfo(SysUserDto sysUserDto) {
        if (sysUserDto.getId() == null || "".equals(sysUserDto.getId())){
            sysUserDto.setId(IdUtil.simpleUUID());
            sysUserDto.setCreateTime(new Date());
        }

        sysUserDto.setUpdateTime(new Date());
        return sysUserDto;
    }


}
